package com.course.server.service;/**
 * @author: lin
 */

import com.course.server.domain.File;
import com.course.server.domain.FileExample;
import com.course.server.dto.FileDto;
import com.course.server.dto.PageDto;
import com.course.server.mapper.FileMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
 *@Author lin
 *@Data Created in 2020/7/28 21:21
 *Description 
 *@Modified By
 *@Version V1.0
 */
@Service
public class FileService {
    @Resource
    private FileMapper fileMapper;
    /**
     * create by: lin
     * description: 列表查询
     * create time: 2020/8/2 17:40
     */
    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        FileExample fileExample = new FileExample();
        List<File> fileList = fileMapper.selectByExample(fileExample);
        PageInfo<File> pageInfo = new PageInfo<>(fileList);
        pageDto.setTotal(pageInfo.getTotal());
        List<FileDto> fileDtoList = CopyUtil.copyList(fileList, FileDto.class);
        pageDto.setList(fileDtoList);
    }
    /**
     * create by: lin
     * description: 保存
     * create time: 2020/8/2 17:40
     */
    public void save(FileDto fileDto){
        File file = CopyUtil.copy(fileDto, File.class);
        File fileDb = selectByKey(fileDto.getKey());
        if(fileDb == null){
            //StringUtils.isEmpty spring的工具类来判断是否有值
            this.insert(file);
        }else {
            fileDb.setShardIndex(fileDto.getShardIndex());
            this.update(fileDb);
        }
    }
    /**
     * create by: lin
     * description: 新增
     * create time: 2020/8/2 17:41
     */
    private void insert(File file){
            Date now = new Date();
            file.setCreatedAt(now);
            file.setUpdatedAt(now);
        file.setId(UuidUtil.getShortUuid());
        fileMapper.insert(file);
    }
    /**
     * create by: lin
     * description: 修改
     * create time: 2020/8/2 17:58
     */
    private void update(File file){
            file.setUpdatedAt(new Date());
        fileMapper.updateByPrimaryKey(file);
    }
    /**
     * create by: lin
     * description: 删除
     * create time: 2020/8/2 17:41
     */
    public void delete(String id){
        fileMapper.deleteByPrimaryKey(id);
    }
    public File selectByKey(String key){
        FileExample fileExample = new FileExample();
        fileExample.createCriteria().andKeyEqualTo(key);
        List<File> fileList = fileMapper.selectByExample(fileExample);
        if(CollectionUtils.isEmpty(fileList)){
            return null;
        }else {
            return fileList.get(0);
        }
    }
    /**
     * 根据文件标识查询数据库记录
     */
    public FileDto findByKey(String key) {
        return CopyUtil.copy(selectByKey(key), FileDto.class);
    }
}
